package com.sykj.cloud.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.system.domain.Organization;
import com.sykj.cloud.system.service.IOrganizationService;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.page.TableDataInfo;

/**
 * 组织机构Controller
 *
 * @author mr.will
 * @date 2021-03-15
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController extends BaseController
{
    @Autowired
    private IOrganizationService organizationService;

    /**
     * 新增组织机构
     */
    @PreAuthorize(hasPermi = "system:organization:add")
    @Log(title = "组织机构", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Organization organization)
    {
        return toAjax(organizationService.insertOrganization(organization));
    }

    /**
     * 修改组织机构
     */
    @PreAuthorize(hasPermi = "system:organization:edit")
    @Log(title = "组织机构", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Organization organization)
    {
        return toAjax(organizationService.updateOrganization(organization));
    }

    /**
     * 删除组织机构
     */
    @PreAuthorize(hasPermi = "system:organization:remove")
    @Log(title = "组织机构", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(organizationService.deleteOrganizationByIds(guids));
    }

    /**
     * 获取组织机构详细信息
     */
    @PreAuthorize(hasPermi = "system:organization:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(organizationService.getOrganizationById(guid));
    }

    /**
     * 导出组织机构列表
     */
    @PreAuthorize(hasPermi = "system:organization:export")
    @Log(title = "组织机构", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Organization organization) throws IOException
    {
        List<Organization> list = organizationService.listOrganizations(organization);
        ExcelUtil<Organization> util = new ExcelUtil<Organization>(Organization.class);
        util.exportExcel(response, list, "organization");
    }

    /**
     * 查询组织机构列表
     */
    @PreAuthorize(hasPermi = "system:organization:list")
    @GetMapping("/list")
    public TableDataInfo list(Organization organization)
    {
        startPage();
        List<Organization> list = organizationService.listOrganizations(organization);
        return getDataTable(list);
    }

    /**
     * 查询组织机构信息，下拉框值
     */
    @PreAuthorize(hasPermi = "system:organization:list")
    @GetMapping("/option/list")
    public AjaxResult optionList(Organization organization)
    {
        return AjaxResult.success(organizationService.listOrganizations(organization));
    }
}
